package com.ntschy.sys.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ntschy.common.constant.Constants;
import com.ntschy.common.core.entity.dataobject.SysConfig;
import com.ntschy.common.core.redis.RedisCache;
import com.ntschy.sys.mapper.SysConfigMapper;
import com.ntschy.sys.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.List;

/**
 * @author ezreal_geng
 */
@Service
public class SysConfigServiceImpl implements ISysConfigService {

    @Autowired
    private SysConfigMapper configMapper;

    @Autowired
    private RedisCache redisCache;

    /**
     * 项目启动时，初始化参数到缓存
     */
    @PostConstruct
    public void init() {
        List<SysConfig> configsList = configMapper.selectList(null);
        for (SysConfig config : configsList) {
            redisCache.setCacheObject(Constants.SYS_CONFIG_KEY + config.getConfigKey(), config.getConfigValue());
        }
    }

    // * ================================================基础增删改查BEGIN============================================= *
    @Override
    public List<SysConfig> pageConfig(SysConfig config) {
        return configMapper.pageConfig(config);
    }

    @Override
    public int addConfig(SysConfig config) {
        config.setId(IdUtil.fastUUID());
        int row = configMapper.insert(config);
        if(row > 0) {
            redisCache.setCacheObject(Constants.SYS_CONFIG_KEY + config.getConfigKey(), config.getConfigValue());
        }
        return row;
    }

    @Override
    public int updateConfig(SysConfig config) {
        config.setId(IdUtil.fastUUID());
        int row = configMapper.updateById(config);
        if(row > 0) {
            redisCache.setCacheObject(Constants.SYS_CONFIG_KEY + config.getConfigKey(), config.getConfigValue());
        }
        return row;
    }

    @Override
    public int deleteConfig(String id) {
        SysConfig oldConfig = configMapper.selectById(id);
        if(ObjectUtil.isNotNull(oldConfig)) {
            int row = configMapper.deleteById(id);
            if(row > 0) {
                redisCache.deleteObject(Constants.SYS_CONFIG_KEY + oldConfig.getConfigKey());

            }
            return row;
        } else {
            return 0;
        }
    }

    @Override
    public SysConfig fetchConfig(String id) {
        return configMapper.selectById(id);
    }

    @Override
    public SysConfig fetchConfigByKey(String key) {
        SysConfig cacheConfig = (SysConfig)redisCache.getCacheObject(Constants.SYS_CONFIG_KEY + key);
        if(ObjectUtil.isNotNull(cacheConfig)) {
            return cacheConfig;
        }
        return configMapper.selectOne(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getConfigKey, key));
    }

    // * =================================================基础增删改查END============================================== *


    @Override
    public SysConfig checkConfigNameExist(String configName) {
        return configMapper.selectOne(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getConfigName, configName));
    }

    @Override
    public SysConfig checkConfigKeyExist(String configKey) {
        return configMapper.selectOne(new QueryWrapper<SysConfig>().lambda().eq(SysConfig::getConfigKey, configKey));
    }
}
